package com.hcl.orm.core.exception;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.TypeUtil;
import com.hcl.orm.core.annotation.SqlCommandType;
import com.hcl.orm.core.dialect.Signature;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;

/**
 * @author hucl
 * @projectName jim-orm
 * @description: TODO
 * @date 2021/5/1917:06
 */
public class ParamAssert {

    public static Boolean isCollection(Method method, int index) {
        return Collection.class.isAssignableFrom(TypeUtil.getParamClass(method, index));
    }

    public static void hasValidParamWhileSimpleFunction(SqlCommandType commandType, Signature signature) {
        Method method = signature.getMethod();
        if (SqlCommandType.INSERT.equals(commandType)) {
            if (method.getParameterCount() != 1
                    || ClassUtil.isBasicType(TypeUtil.getFirstParamClass(method))
            ) {
                throw new OrmException("method " + method.getName() + " params is invalid!");
            }
        }
    }

    /**
     * @param params 实际参数列表
     * @param expire 预期的数量
     */
    public static void checkHsFunctionParam(List<String> params, int expire) {
        if (params == null ||
                params.size() != expire) {
            throw new ValidFunctionParamException("the function has wrong number params,should be " + expire);
        }
    }
}
